package com.cyf.restTemplate.controller;

import com.cyf.common.web.common.Rs;
import com.cyf.common.web.util.HttpRequestUtil;
import com.cyf.common.web.util.HttpResponseUtil;
import com.cyf.restTemplate.entity.BookDto;
import com.cyf.restTemplate.entity.UserDto;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chenyifan
 * @create 2025-04-07 10:02
 */
@RequestMapping("/test")
@RestController
public class RestTemplateController {

    public static List<BookDto> bookList = Arrays.asList(
            new BookDto(1, "SpringMVC"),
            new BookDto(2, "SpringBoot"),
            new BookDto(3, "Spring")
    );


    @GetMapping("/get")
    public List<BookDto> get() {
        return bookList;
    }

    @GetMapping("/get/{id}/{name}")
    public Rs get(@PathVariable("id") Integer id, @PathVariable("name") String name) {
        return Rs.ok().setData(bookList).set("id", id).set("name", name);
    }

    @GetMapping("/header")
    public Rs header() {
        Map<String, String> headers = HttpRequestUtil.getHeaders();
        return Rs.ok().set("headers", headers);
    }

    @GetMapping("/get/all/{id}")
    public Rs getAll(@PathVariable("id") Integer id, @RequestParam("name") String name, @RequestParam("age") Integer age, @RequestHeader("token") String token) {
        return Rs.ok().set("id", id).set("name", name).set("token", token).set("age", age);
    }

    @PostMapping("/downFile")
    public void downFile() {
        HttpResponseUtil.downLocalFile("1.png", "classpath:static/png/1.png");
    }

    // application/x-www-form-urlencoded
    @PostMapping("/post/form1")
    public BookDto postForm1(BookDto bookDto) {
        return bookDto;
    }

    @PostMapping("/post/form2")
    public Rs postForm2(@RequestParam("file") MultipartFile multipartFile) {
        return Rs.ok().setData(fileInfo(multipartFile));
    }

    @PostMapping("/post/form3")
    public Rs postForm3(UserDto userDto) {
        return Rs.ok().set("name", userDto.getName())
                .set("image", fileInfo(userDto.getImage()))
                .set("images", userDto.getImages().stream().map(this::fileInfo).collect(Collectors.toList()));
    }

    private Map<String, String> fileInfo(MultipartFile multipartFile) {
        HashMap<String, String> map = new HashMap<>();
        map.put("fileName", multipartFile.getOriginalFilename());
        map.put("fileSize", String.valueOf(multipartFile.getSize()));
        map.put("fileType", multipartFile.getContentType());
        return map;
    }

    @PostMapping("/post/json1")
    public Rs postJson1(@RequestBody BookDto bookDto) {
        return Rs.ok().setData(bookDto);
    }

    @PostMapping("/post/json2")
    public List<BookDto> postJson2(@RequestBody List<BookDto> list) {
        return list;
    }
}